package com.vehiclemanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vehiclemanagement.entity.Driver;
import com.vehiclemanagement.service.DriverService;
import com.vehiclemanagement.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import java.util.List;

/**
 * 驾驶员管理控制器
 */
@Api(tags = "驾驶员管理", description = "驾驶员信息的增删改查接口")
@RestController
@RequestMapping("/driver")
public class DriverController {
    
    @Autowired
    private DriverService driverService;
    
    /**
     * 分页查询驾驶员列表
     */
    @ApiOperation(value = "分页查询驾驶员列表", notes = "根据条件分页查询驾驶员信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "Integer", paramType = "query", defaultValue = "1"),
        @ApiImplicitParam(name = "size", value = "每页数量", required = true, dataType = "Integer", paramType = "query", defaultValue = "10"),
        @ApiImplicitParam(name = "searchTerm", value = "搜索关键词（姓名、手机号等）", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "status", value = "状态（在职、请假、休息、离职）", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "includeDeleted", value = "是否包含已删除记录", required = false, dataType = "Boolean", paramType = "query", defaultValue = "false")
    })
    @ApiResponses({
        @ApiResponse(code = 200, message = "操作成功"),
        @ApiResponse(code = 500, message = "操作失败")
    })
    @GetMapping("/list")
    public R<IPage<Driver>> list(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "searchTerm", required = false) String searchTerm,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "includeDeleted", required = false) Boolean includeDeleted) {
        IPage<Driver> page = driverService.pageDrivers(current, size, searchTerm, status, includeDeleted);
        return R.ok(page);
    }
    
    /**
     * 获取驾驶员详情
     */
    @ApiOperation(value = "获取驾驶员详情", notes = "根据ID获取驾驶员详细信息")
    @ApiImplicitParam(name = "id", value = "驾驶员ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses({
        @ApiResponse(code = 200, message = "操作成功"),
        @ApiResponse(code = 404, message = "驾驶员不存在"),
        @ApiResponse(code = 500, message = "操作失败")
    })
    @GetMapping("/{id}")
    public R<Driver> getById(@PathVariable Long id) {
        Driver driver = driverService.getDriverById(id);
        if (driver == null) {
            return R.error("驾驶员不存在");
        }
        return R.ok(driver);
    }
    
    /**
     * 添加驾驶员
     */
    @ApiOperation(value = "添加驾驶员", notes = "添加新的驾驶员信息")
    @ApiResponses({
        @ApiResponse(code = 200, message = "添加成功"),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 500, message = "添加失败")
    })
    @PostMapping
    public R<Boolean> add(@ApiParam(value = "驾驶员信息", required = true) @RequestBody Driver driver) {
        System.out.println("接收到添加驾驶员请求，数据：" + driver);
        
        // 检查必要字段
        if (driver.getName() == null || driver.getName().trim().isEmpty()) {
            return R.error("驾驶员姓名不能为空");
        }
        
        boolean result = driverService.addDriver(driver);
        if (result) {
            return R.ok(true);
        } else {
            System.out.println("添加驾驶员失败，服务层返回false");
            return R.error("添加失败");
        }
    }
    
    /**
     * 更新驾驶员
     */
    @ApiOperation(value = "更新驾驶员", notes = "根据ID更新驾驶员信息")
    @ApiResponses({
        @ApiResponse(code = 200, message = "更新成功"),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 500, message = "更新失败")
    })
    @PutMapping
    public R<Boolean> update(@ApiParam(value = "驾驶员信息", required = true) @RequestBody Driver driver) {
        if (driver.getId() == null) {
            return R.error("驾驶员ID不能为空");
        }
        boolean result = driverService.updateDriver(driver);
        return result ? R.ok(true) : R.error("更新失败");
    }
    
    /**
     * 删除驾驶员（物理删除）
     */
    @ApiOperation(value = "删除驾驶员", notes = "根据ID删除驾驶员信息")
    @ApiImplicitParam(name = "id", value = "驾驶员ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses({
        @ApiResponse(code = 200, message = "删除成功"),
        @ApiResponse(code = 500, message = "删除失败")
    })
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        System.out.println("接收到删除驾驶员请求，ID: " + id);
        
        boolean result = driverService.deleteDriver(id);
        
        if (result) {
            System.out.println("成功删除驾驶员，ID: " + id);
            return R.ok(true);
        } else {
            System.out.println("删除驾驶员失败，ID: " + id);
            return R.error("删除失败");
        }
    }
    
    /**
     * 获取部门列表
     */
    @ApiOperation(value = "获取部门列表", notes = "获取系统中所有的部门列表")
    @ApiResponses({
        @ApiResponse(code = 200, message = "操作成功"),
        @ApiResponse(code = 500, message = "操作失败")
    })
    @GetMapping("/departments")
    public R<List<String>> getDepartments() {
        List<String> departments = driverService.getDepartments();
        return R.ok(departments);
    }
    
    /**
     * 测试接口
     */
    @ApiOperation(value = "测试接口", notes = "测试驾驶员管理API是否正常运行")
    @GetMapping("/test")
    public R<String> test() {
        return R.ok("驾驶员管理API正常运行！");
    }
} 